<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <!-- 
    sessionStorage能存5M
    localStorage能存20M
    两者只能存储字符串，可以将对象JSON.stringify()编码后存储
      window.sessionStorage的生命周期
        1.关闭浏览器即结束
        2.在同一个窗口或页面下是可以共享的
        3.以键值对形式来存储使用
   -->
  <input type="text">
  <button class="set">存取数据</button>
  <button class="get">获取数据</button>
  <button class="remove">删除数据</button>
  <button class="del">清空所有的数据</button>
  <script>
    let ipt = document.querySelector("input");
    let set = document.querySelector(".set");
    let get = document.querySelector(".get");
    let removes = document.querySelector(".remove");
    let dels = document.querySelector(".del");

    set.addEventListener("click", () => {
      // 当点击后把表单里面的数据存放起来
      let val = ipt.value;
      sessionStorage.setItem("uname", val);
    });

    get.addEventListener("click", () => {
      let unames = sessionStorage.getItem("uname");
      console.log(unames);
    });

    removes.addEventListener("click", () => {
      sessionStorage.removeItem("uname");
    });

    // 清除所有
    dels.addEventListener("click", () => {
      sessionStorage.clear();
    });
  </script>
</body>

</html>